package com.wudl.realtime.util;

/**
 * @author ：wudl
 * @date ：Created in 2022-01-05 23:26
 * @description：
 * @modified By：
 * @version: 1.0
 */

import com.wudl.realtime.config.QuotConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;

/**
 * Redis工具类
 */
public class RedisUtil {

    //1.新建获取连接的方法
    public static JedisCluster getJedisCluster(){

        //2.初始化连接池
        JedisPoolConfig config = new JedisPoolConfig();
        String host = QuotConfig.config.getProperty("redis.host");
        String maxTotal = QuotConfig.config.getProperty("redis.maxTotal");
        String minIdle = QuotConfig.config.getProperty("redis.minIdle");
        String maxIdle = QuotConfig.config.getProperty("redis.maxIdle");

        config.setMaxTotal(Integer.parseInt(maxTotal));
        config.setMinIdle(Integer.parseInt(minIdle));
        config.setMaxIdle(Integer.parseInt(maxIdle));

        //3.设置连接集群地址
        HashSet<HostAndPort> set = new HashSet<>();
        String[] arr = host.split(",");
        for (String line : arr) {
            String[] strArr = line.split(":");
            set.add(new HostAndPort(strArr[0],Integer.parseInt(strArr[1])));
        }
        //4.获取客户端连接对象
        JedisCluster jedisCluster = new JedisCluster(set, config);
        return jedisCluster;
    }

    public static void main(String[] args) throws IOException {

        JedisCluster jedisCluster = getJedisCluster();
        jedisCluster.hset("quot", "zf", "-1") ;//振幅
        jedisCluster.hset("quot", "upDown1", "-1"); //涨跌幅-跌幅
        jedisCluster.hset("quot", "upDown2", "100") ;//涨跌幅-涨幅
        jedisCluster.hset("quot", "hsl", "-1") ;//换手率
        jedisCluster.close();
    }
}